import { ref } from 'vue'
import { defineStore } from 'pinia'

export const useDictStore = defineStore('dict', () => {
  const STORAGE_KEY = 'erppos_dict'
  const colors = ref([])
  const sizes = ref([])
  const tags = ref([])

  const initialize = () => {
    const cached = localStorage.getItem(STORAGE_KEY)
    if (cached) {
      const data = JSON.parse(cached)
      colors.value = data.colors || []
      sizes.value = data.sizes || []
      tags.value = data.tags || []
      return
    }
    colors.value = ['黑色', '白色', '红色', '蓝色', '灰色']
    sizes.value = ['S', 'M', 'L', 'XL', 'XXL']
    tags.value = ['标准吊牌', '促销价签', '清仓价签']
    persist()
  }

  const persist = () => {
    localStorage.setItem(STORAGE_KEY, JSON.stringify({ colors: colors.value, sizes: sizes.value, tags: tags.value }))
  }

  // 通用增删
  const addColor = (v) => { if (!v) return; if (!colors.value.includes(v)) colors.value.push(v); persist() }
  const removeColor = (v) => { const i = colors.value.indexOf(v); if (i>-1) colors.value.splice(i,1); persist() }
  const addSize = (v) => { if (!v) return; if (!sizes.value.includes(v)) sizes.value.push(v); persist() }
  const removeSize = (v) => { const i = sizes.value.indexOf(v); if (i>-1) sizes.value.splice(i,1); persist() }
  const addTag = (v) => { if (!v) return; if (!tags.value.includes(v)) tags.value.push(v); persist() }
  const removeTag = (v) => { const i = tags.value.indexOf(v); if (i>-1) tags.value.splice(i,1); persist() }

  return { colors, sizes, tags, initialize, addColor, removeColor, addSize, removeSize, addTag, removeTag }
})


